<div class="alert-title">添加层级</div>

<div class="layer-info">
<div class="am-panel am-panel-primary">
    <div class="am-panel-hd">条件</div>
    <div class="am-panel-bd">
        <table width="100%">
            <tr>
                <td>层级ID：</td>
                <td><input type="number" name="layer_id" /></td>
                <td>层级名称：</td>
                <td><input type="text" name="layer_name" /></td>
            </tr>
            <tr>
                <td>充值次数：</td>
                <td><input type="number" name="layer_deposit_count" /></td>
                <td>充值总额：</td>
                <td><input type="number" name="layer_deposit_sum" /></td>
            </tr>
            <tr>
                <td>最大充值金额：</td>
                <td><input type="number" name="layer_deposit_max" /></td>
                <td colspan="2"></td>
            </tr>
            <tr>
                <td>提现次数：</td>
                <td><input type="number" name="layer_withdraw_count" /></td>
                <td>提现总额：</td>
                <td><input type="number" name="layer_withdraw_sum" /></td>
            </tr>
            <tr>
                <td>注册开始时间：</td>
                <td><input type="text" name="layer_register_start" /></td>
                <td>注册结束时间</td>
                <td><input type="text" name="layer_register_end" /></td>
            </tr>
            <tr>
                <td>备注：</td>
                <td><input type="text" name="layer_memo" /></td>
                <td colspan="2"></td>
            </tr>
        </table>
    </div>
</div>

<div class="am-panel am-panel-primary">
    <div class="am-panel-hd">限制</div>
    <div class="am-panel-bd">
        <table width="100%">
            <tr>
                <td>每天最大提现次数：</td>
                <td><input type="number" name="layer_xz_daycount" /></td>
                <td>每天最大提现金额：</td>
                <td><input type="number" name="layer_xz_daysum" /></td>
            </tr>
            <tr>
                <td>单笔最小提现金额：</td>
                <td><input type="number" name="layer_xz_min" /></td>
                <td>单笔最大提现金额：</td>
                <td><input type="number" name="layer_xz_max" /></td>
            </tr>
        </table>
    </div>
</div>

<div class="button_line">
    <button class="btn-save common_button admin_button">保存</button>
    <button class="close common_button admin_button">取消</button>
</div>
</div>
<style type="text/css">
    .layer-info table td {
        text-align: right;
        padding: 5px;
    }
    .am-panel-bd { padding: 5px; }
</style>

<script>
    $("input[name='layer_register_start']").datepicker({format: 'yyyy-mm-dd'});
    $("input[name='layer_register_end']").datepicker({format: 'yyyy-mm-dd'});
    $(".layer-info .close").click(function() {
        $("#add_layer").remove();
    })
    $(".layer-info .btn-save").click(function() {
        var data = {};
        var v = $("input[name='layer_id']").val().trim();
        if (v == '') {
            alert2("请输入层级ID", ture); return;
        }
        data.level = v;

        var v = $("input[name='layer_name']").val().trim();
        if (!v) {
            alert2("请输入层级名称", ture); return;
        }
        data.name = v;
        data.memo = $("input[name='layer_memo']").val().trim() || '';

        var v = $("input[name='layer_deposit_count']").val();
        if (v == '') {
            alert2("请输入充值次数", ture); return;
        }
        data.depositCount = parseInt(v);

        var v = $("input[name='layer_deposit_sum']").val();
        if (v == '') {
            alert2("请输入充值总额", ture); return;
        }
        data.depositSum = parseInt(v) * 100;

        var v = $("input[name='layer_deposit_max']").val();
        if (v == '') {
            alert2("请输入最大充值总额", ture); return;
        }
        data.depositMax = parseInt(v) * 100;

        var v = $("input[name='layer_withdraw_count']").val();
        if (v == '') {
            alert2("请输入取款次数", ture); return;
        }
        data.withdrawCount = parseInt(v);

        var v = $("input[name='layer_withdraw_sum']").val();
        if (v == '') {
            alert2("请输入取款总额", ture); return;
        }
        data.withdrawSum = parseInt(v) * 100;

        data.registerStart = new Date($("input[name='layer_register_start']").val()).getTime();
        data.registerEnd = new Date($("input[name='layer_register_end']").val()).getTime();

        var v = $("input[name='layer_xz_daycount']").val();
        if (v == '') {
            alert2("请输入每天最大提现次数", ture); return;
        }
        data.dayWithdrawCount = parseInt(v);

        var v = $("input[name='layer_xz_daysum']").val();
        if (v == '') {
            alert2("请输入每天最大提现金额", ture); return;
        }
        data.dayWithdrawSum = parseInt(v) * 100;

        var v = $("input[name='layer_xz_min']").val();
        if (v == '') {
            alert2("请输入每天最小提现金额", ture); return;
        }
        data.withdrawMin = parseInt(v) * 100;

        var v = $("input[name='layer_xz_max']").val();
        if (v == '') {
            alert2("请输入每天最大提现金额", ture); return;
        }
        data.withdrawMax = parseInt(v) * 100;
        if (context.center.layerOper == "add") {
            startLoading();
            if (!Layer.add(data, function() {
                stopLoading();
                $("#add_layer").remove();
                queryLayers();
            })) return;
        }
        else if (context.center.layerOper == "edit") {
            startLoading();
            Layer.save(data, function() {
                stopLoading();
                $("#add_layer").remove();
                queryLayers();
            });
        }
    })

    if (context.center.layerOper == "add") {
        // 添加层级
        $(".alert-title").html("添加层级");
    }
    else if (context.center.layerOper == "edit") {
        // 添加层级
        $(".alert-title").html("修改层级");

        // ID不能修改
        $("input[name='layer_id']").attr("disabled", "disabled");
    }
    else if (context.center.layerOper == "detail") {
        $(".alert-title").html("查看层级");
        $(".layer-info .btn-save").hide();
    }

    // 填充内容
    function fillLayerData() {
        var d = context.center.layer;
        if (!d) return;
        $("input[name='layer_id']").val(d.level || 0);
        $("input[name='layer_name']").val(d.name || '');
        $("input[name='layer_memo']").val(d.memo || '');
        $("input[name='layer_deposit_count']").val(d.depositCount || 0);
        $("input[name='layer_deposit_sum']").val(d.depositSum/100);
        $("input[name='layer_deposit_max']").val(d.depositMax / 100);
        $("input[name='layer_withdraw_count']").val(d.withdrawCount || 0);
        $("input[name='layer_withdraw_sum']").val(d.withdrawSum/100);

        $("input[name='layer_register_start']").datepicker("setValue", new Date(d.registerStart).Format("yyyy-MM-dd"));
        $("input[name='layer_register_end']").datepicker("setValue", new Date(d.registerEnd).Format("yyyy-MM-dd"));

        $("input[name='layer_xz_daycount']").val(d.dayWithdrawCount);
        $("input[name='layer_xz_daysum']").val(d.dayWithdrawSum/100);
        $("input[name='layer_xz_min']").val(d.withdrawMin/100);
        $("input[name='layer_xz_max']").val(d.withdrawMax/100);
    }
    fillLayerData();
</script>